package de.dehuegel;

import java.util.Timer;

import android.content.Context;
import android.graphics.*;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainView extends View implements View.OnTouchListener{

	private final int TOP_DISTANCE =10 ;
	private final int DOWN_DISTANCE =10;
	private final int RIGHT_DISTANCE =10;
	private final int LEFT_DISTANCE =10;
	private final int PLAYINGFIELD_WIDTH=10;
	private final int PLAYINGFIELD_HEIGHT=20;

	private Game game;
	private Timer timer;
	private int width;
	private int height;

	public MainView(Context context) {
		super(context);
		this.setOnTouchListener(this);
		this.game = new Game(new PlayingField(PLAYINGFIELD_HEIGHT, PLAYINGFIELD_WIDTH));
		this.timer= new Timer();
	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.width = MeasureSpec.getSize(widthMeasureSpec);
		this.height = MeasureSpec.getSize(heightMeasureSpec);
		this.setMeasuredDimension(this.width , this.height);
	}
	

	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);

		Paint paint = new Paint();
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);
		canvas.drawPaint(paint);
		drawTitle(canvas);
		drawGrid(canvas);
		drawScore(canvas);
		drawStartButton(canvas);
	}
	
	private void drawStartButton(Canvas canvas) {
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.start);
		int y = (int) (this.height-(this.height*(DOWN_DISTANCE/100.0)));
		int x = (int) (this.width-(this.width*(LEFT_DISTANCE/100.0)));
		
		int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int newWidth = (int) (this.width*(LEFT_DISTANCE/100.0));
        int newHeight =  (int) (this.height*(DOWN_DISTANCE/100.0));
        
        float scaleWidth = (float) newWidth / width;
        float scaleHeight = (float) newHeight / height;
        
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,width, height, matrix, true);
        canvas.drawBitmap(resizedBitmap , x,y, null);
	}

	private void drawScore(Canvas canvas){
		
	}

	private void drawTitle(Canvas canvas){
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.title);
		int width = bitmap.getWidth();
        int height = bitmap.getHeight();
     
        int newWidth = canvas.getWidth();
        int newHeight = (int) ((TOP_DISTANCE/100.0)*canvas.getHeight());

        float scaleWidth = (float) newWidth / width;
        float scaleHeight = (float) newHeight / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,width, height, matrix, true);
        canvas.drawBitmap(resizedBitmap, new Matrix(), null);
	}

	private void drawGrid(Canvas canvas){
		int squareSize=getSquareSize();
		int gridHeight=getGridHeight(squareSize);
		int gridWidht=getGridWidth(squareSize);

		Point2D topLeft = getTopLeftPosition(squareSize,gridHeight,gridWidht);
		Point2D topRight = getTopRightPosition(squareSize,gridHeight,gridWidht);
		Point2D downLeft = getDownLeftPosition(squareSize,gridHeight,gridWidht);
		Point2D downRight = getDownRightPosition(squareSize,gridHeight,gridWidht);

		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.FILL);


		for(int i=0;i<=PLAYINGFIELD_HEIGHT;i++){
			canvas.drawLine(topLeft.getX(), topLeft.getY()+(i*squareSize), topRight.getX(), topRight.getY()+(i*squareSize), paint);
		}
		for(int i=0;i<=PLAYINGFIELD_WIDTH;i++){
			canvas.drawLine(topLeft.getX()+(i*squareSize), topLeft.getY(), downLeft.getX()+(i*squareSize), downLeft.getY(), paint);
		}

	}

	private int getGridHeight(int squareSize){
		return squareSize*PLAYINGFIELD_HEIGHT;	
	}

	private int getGridWidth(int squareSize){
		return squareSize*PLAYINGFIELD_WIDTH;
	}

	private Point2D getTopLeftPosition(int squareSize,int gridHeight,int gridWidth){

		int x = (this.width -gridWidth) / 2;
		int y = (this.height - gridHeight) / 2;		
		return new Point2D(x, y);
	}

	private Point2D getTopRightPosition(int squareSize,int gridHeight,int gridWidth){
		int x = ((this.width -gridWidth) / 2)+gridWidth;
		int y = (this.height - gridHeight) / 2;	
		return new Point2D(x, y);

	}

	private Point2D getDownLeftPosition(int squareSize,int gridHeight,int gridWidth){
		int x = (this.width -gridWidth) / 2;
		int y = ((this.height - gridHeight) / 2)+gridHeight;			
		return new Point2D(x, y);
	}

	private Point2D getDownRightPosition(int squareSize,int gridHeight,int gridWidth){
		int x = ((this.width -gridWidth) / 2)+gridWidth;
		int y = ((this.height - gridHeight) / 2)+gridHeight;
		return new Point2D(x, y);
	}

	private int getSquareSize() {
		int xSpace = (int) ((this.width-((this.width * (this.LEFT_DISTANCE/100.0)) + (this.width * (this.RIGHT_DISTANCE/100.0))))/PLAYINGFIELD_WIDTH);
		int ySpace = (int) ((this.height-((this.height * (this.TOP_DISTANCE/100.0)) + (this.height * (this.DOWN_DISTANCE/100.0))))/PLAYINGFIELD_HEIGHT);

		return (xSpace>ySpace) ? ySpace : xSpace;		
	}

	public boolean onTouch(View v, MotionEvent event) {
		if(isStartButtonPressed(new Point2D( (int) event.getX(),(int) event.getY()))){
			this.game.start();
			Toast.makeText(getContext(), "Start", Toast.LENGTH_SHORT).show();
		}
		return true;
	}
	
	private boolean isStartButtonPressed(Point2D point){
		int y = (int) (this.height-(this.height*(DOWN_DISTANCE/100.0)));
		int x = (int) (this.width-(this.width*(LEFT_DISTANCE/100.0)));
		return ((point.getX()>=x && point.getX()<=this.width) && (point.getY()>=y && point.getY()<=this.height));
	}
}
